package com.fansl.allround.auth.endpoint;

import com.fansl.allround.auth.provider.social.SysSocialUserSignUp;
import com.fansl.allround.auth.provider.social.UcenterSocialUserSignUp;
import com.fansl.allround.common.core.constant.enums.ServiceTypeEnum;
import com.fansl.allround.common.core.util.R;
import com.fansl.allround.common.security.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author fansl
 * @Description: TODO
 * @date 2020/1/8 14:08
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private SysSocialUserSignUp sysSocialUserSignUp;
    @Autowired
    private UcenterSocialUserSignUp ucenterSocialUserSignUp;


    /**
     * 绑定第三方应用
     *
     * @param oauthId     三方应用ID
     * @param code        三方应用code
     * @param serviceType 服务类型
     * @return
     */
    @PostMapping("/bind/{oauthId}")
    public R bindSocialInfo(@PathVariable Integer oauthId,
                            @RequestParam String code,
                            @RequestParam ServiceTypeEnum serviceType) {
        Integer userId = SecurityUtils.getUser().getId();
        switch (serviceType) {
            case BACKEND:
                return new R(sysSocialUserSignUp.bindSocialUser(userId, oauthId, code));
            default:
                break;
        }
        return new R(false);
    }

    /**
     * 解绑第三方应用
     *
     * @param oauthId
     * @param serviceType
     * @return
     */
    @PostMapping("/unbind/{oauthId}")
    public R unbindSocialInfo(@PathVariable Integer oauthId,
                              @RequestParam ServiceTypeEnum serviceType) {
        Integer userId = SecurityUtils.getUser().getId();
        switch (serviceType) {
            case BACKEND:
                return new R(sysSocialUserSignUp.unbindSocialUser(userId, oauthId));
            default:
                break;
        }
        return new R(false);
    }
}
